package com.bwie.task;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.domain.TbCoupon;
import com.bwie.es.EsCoupon;
import com.bwie.es.EsUser;
import com.bwie.mapper.TbCouponMapper;
import com.bwie.mapper.TbUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : xiaogao
 * @date : 2024-11-21 18:38
 * @description :
 **/
@Component
public class SyncProdTask {
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    TbCouponMapper tbCouponMapper;

//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void syncProd() {
//        System.out.println("同步商品信息到ES中");
//        // 判断es索引是否存在 ，如果不存在 创建索引
//        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(EsUser.class);
//        boolean exists = indexOperations.exists();
//        if(!exists){
//            // 索引不存在 创建索引
//            indexOperations.create();
//            // 创建静态映射
//            Document mapping = indexOperations.createMapping();
//            indexOperations.putMapping(mapping);
//        }
//        // 获取列表，循环遍历，调用ES中的API 添加数据到ES中
//        List<TbCoupon> tbCoupons = tbCouponMapper.selectList(null);
//        for (TbCoupon tbCoupon : tbCoupons) {
//            EsCoupon esCoupon = BeanUtil.toBean(tbCoupon, EsCoupon.class);
//            elasticsearchRestTemplate.save(esCoupon);
//        }
//        System.out.println("同步商品信息到ES中完成");
//    }
}